package cn.efunbox.afw.data.redis.serialize;

import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Created by Dreampie on 16/9/2.
 */
public class JdkSerializationRedisStringSerializer extends JdkSerializationRedisSerializer {
  public static StringRedisSerializer stringSerializer = new StringRedisSerializer();

  public Object deserialize(byte[] bytes) {
    try {
      return super.deserialize(bytes);
    } catch (Exception var3) {
      try {
        return stringSerializer.deserialize(bytes);
      } catch (Exception e) {
        throw new SerializationException("Cannot deserialize", e);
      }
    }
  }

  public byte[] serialize(Object object) {
    if (object instanceof String) {
      try {
        return (byte[]) stringSerializer.serialize((String) object);
      } catch (Exception e) {
        throw new SerializationException("Cannot serialize", e);
      }
    } else {
      return super.serialize(object);
    }
  }
}
